<?php
declare (strict_types=1);

namespace app\controller;

use app\model\Challenge as ChallengeModel;
use app\model\ChallengeMember as ChallengeMemberModel;
use think\facade\Request;
use think\facade\Validate;

class Challenge
{
    public function createChallenge()
    {
        $data = Request::post();

        // 验证数据
        $validate = Validate::rule([
            'title' => 'require|max:255',
            'description' => 'max:1000',
            'start_date' => 'require|date',
            'end_date' => 'require|date',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 创建挑战
        $challenge = new ChallengeModel();
        $challenge->user_id = $data['user_id'];  // 从登录状态获取
        $challenge->title = $data['title'];
        $challenge->description = $data['description'];
        $challenge->start_date = $data['start_date'];
        $challenge->end_date = $data['end_date'];
        $challenge->save();

        return json(['message' => '挑战创建成功', 'challenge_id' => $challenge->challenge_id]);
    }

    public function joinChallenge()
    {
        $data = Request::post();

        // 验证数据
        $validate = Validate::rule([
            'challenge_id' => 'require|integer',
            'user_id' => 'require|integer',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 加入挑战
        $member = new ChallengeMemberModel();
        $member->challenge_id = $data['challenge_id'];
        $member->user_id = $data['user_id'];
        $member->save();

        return json(['message' => '成功加入挑战', 'challenge_member_id' => $member->member_id]);
    }

    // 更新挑战
    public function update()
    {
        $data = Request::post();

        $validate = Validate::rule([
            'challenge_id' => 'require|integer',
            'title' => 'require|max:100',
            'description' => 'max:500',
            'start_date' => 'date',
            'end_date' => 'date',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $challenge = ChallengeModel::find($data['challenge_id']);
        if (!$challenge) {
            return json(['status' => 'error', 'message' => 'Challenge not found']);
        }

        $challenge->title = $data['title'];
        $challenge->description = $data['description'] ?? $challenge->description;
        $challenge->start_date = $data['start_date'] ?? $challenge->start_date;
        $challenge->end_date = $data['end_date'] ?? $challenge->end_date;
        $challenge->save();

        return json(['status' => 'success', 'message' => 'Challenge updated successfully']);
    }

    // 退出挑战
    public function leave()
    {
        $data = Request::post();

        $validate = Validate::rule([
            'challenge_id' => 'require|integer',
            'user_id' => 'require|integer',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $member = ChallengeMemberModel::where('challenge_id', $data['challenge_id'])
            ->where('user_id', $data['user_id'])
            ->find();
        if (!$member) {
            return json(['status' => 'error', 'message' => 'Challenge membership not found']);
        }

        $member->delete();

        return json(['status' => 'success', 'message' => 'Left challenge successfully']);
    }
}
